<template>
	<view class="container">
		<view class="user-info-card">
			<!-- 用户基本信息 -->
			<view class="user-info">
				<image class="avatar" :src="userInfo.avatar || '/static/images/default-avatar.png'" mode="aspectFill" @click="changeAvatar"></image>
				<view class="user-details">
					<text class="nickname">{{ userInfo.nickname || '用户昵称' }}</text>
					<text class="user-id">ID: {{ userInfo.id || '--' }}</text>
				</view>
				<view class="edit-btn" @click="navigateTo('/pages/user/edit-profile')">
					<u-icon name="edit-pen" size="34" color="#666"></u-icon>
				</view>
			</view>
			
			<!-- 用户积分 -->
			<view class="user-points">
				<view class="points-title">我的积分</view>
				<view class="points-value">{{ userInfo.points || 0 }}</view>
			</view>
		</view>
		
		<!-- 功能菜单 -->
		<view class="menu-section">
			<view class="section-title">我的服务</view>
			
			<view class="menu-list">
				<view class="menu-item" @click="navigateTo('/pages/order/list')">
					<view class="menu-icon">
						<u-icon name="order" size="48" color="#007aff"></u-icon>
					</view>
					<text class="menu-text">我的订单</text>
				</view>
				
				<view class="menu-item" @click="navigateTo('/pages/address/address')">
					<view class="menu-icon">
						<u-icon name="map" size="48" color="#ff9900"></u-icon>
					</view>
					<text class="menu-text">地址管理</text>
				</view>
				
				<view class="menu-item" @click="navigateTo('/pages/coupon/list')">
					<view class="menu-icon">
						<u-icon name="coupon" size="48" color="#ff6666"></u-icon>
					</view>
					<text class="menu-text">我的优惠券</text>
				</view>
				
				<view class="menu-item" @click="navigateTo('/pages/favorite/list')">
					<view class="menu-icon">
						<u-icon name="heart" size="48" color="#ff6666"></u-icon>
					</view>
					<text class="menu-text">我的收藏</text>
				</view>
			</view>
		</view>
		
		<!-- 账号安全 -->
		<view class="menu-section">
			<view class="section-title">账号设置</view>
			
			<view class="setting-list">
				<view class="setting-item" @click="navigateTo('/pages/user/change-password')">
					<text class="setting-text">修改密码</text>
					<u-icon name="arrow-right" color="#999" size="28"></u-icon>
				</view>
				
				<view class="setting-item" @click="navigateTo('/pages/user/bind-phone')">
					<text class="setting-text">绑定手机</text>
					<view class="setting-value">
						<text>{{ formatPhone(userInfo.phone) }}</text>
						<u-icon name="arrow-right" color="#999" size="28"></u-icon>
					</view>
				</view>
				
				<view class="setting-item" @click="aboutUs">
					<text class="setting-text">关于我们</text>
					<u-icon name="arrow-right" color="#999" size="28"></u-icon>
				</view>
			</view>
		</view>
		
		<!-- 退出登录 -->
		<view class="logout-btn" @click="logout">退出登录</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {}
			}
		},
		onShow() {
			this.getUserInfo();
		},
		methods: {
			// 获取用户信息
			getUserInfo() {
				// 先从本地存储获取
				const userInfoStorage = uni.getStorageSync('userInfo');
				if (userInfoStorage) {
					this.userInfo = userInfoStorage;
				}
				
				// 再从接口获取最新信息
				this.$api.getUserInfo()
					.then(res => {
						this.userInfo = res;
						// 更新本地存储
						uni.setStorageSync('userInfo', res);
					})
					.catch(err => {
						console.error('获取用户信息失败:', err);
					});
			},
			
			// 修改头像
			changeAvatar() {
				uni.chooseImage({
					count: 1,
					sizeType: ['compressed'],
					sourceType: ['album', 'camera'],
					success: (res) => {
						const tempFilePaths = res.tempFilePaths;
						
						// 上传图片
						uni.showLoading({
							title: '上传中...'
						});
						
						this.$api.uploadFile(tempFilePaths[0])
							.then(data => {
								// 更新用户头像
								this.$api.updateUserInfo({
									avatar: data.url
								})
								.then(res => {
									this.userInfo.avatar = data.url;
									// 更新本地存储
									uni.setStorageSync('userInfo', this.userInfo);
									
									uni.hideLoading();
									uni.showToast({
										title: '头像更新成功',
										icon: 'success'
									});
								})
								.catch(err => {
									console.error('更新头像失败:', err);
									uni.showToast({
										title: '更新头像失败',
										icon: 'none'
									});
								});
							})
							.catch(err => {
								uni.hideLoading();
								uni.showToast({
									title: '上传图片失败',
									icon: 'none'
								});
							});
					}
				});
			},
			
			// 格式化手机号
			formatPhone(phone) {
				if (!phone) return '未绑定';
				return phone.replace(/(\d{3})\d{4}(\d{4})/, '$1****$2');
			},
			
			// 页面导航
			navigateTo(url) {
				uni.navigateTo({
					url
				});
			},
			
			// 关于我们
			aboutUs() {
				uni.navigateTo({
					url: '/pages/about/about'
				});
			},
			
			// 退出登录
			logout() {
				uni.showModal({
					title: '提示',
					content: '确定退出登录吗？',
					success: (res) => {
						if (res.confirm) {
							// 清除本地存储的用户信息和token
							uni.removeStorageSync('userInfo');
							uni.removeStorageSync('token');
							
							// 提示
							uni.showToast({
								title: '已退出登录',
								icon: 'success'
							});
							
							// 跳转到登录页
							setTimeout(() => {
								uni.navigateTo({
									url: '/pages/user/login'
								});
							}, 1500);
						}
					}
				});
			}
		}
	}
</script>

<style lang="scss">
	.container {
		padding: 30rpx;
		background-color: #f5f5f5;
		min-height: 100vh;
	}
	
	.user-info-card {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 30rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
		margin-bottom: 30rpx;
	}
	
	.user-info {
		display: flex;
		align-items: center;
		padding-bottom: 30rpx;
		border-bottom: 1px solid #f0f0f0;
	}
	
	.avatar {
		width: 140rpx;
		height: 140rpx;
		border-radius: 50%;
		background-color: #f0f0f0;
	}
	
	.user-details {
		flex: 1;
		padding-left: 30rpx;
	}
	
	.nickname {
		font-size: 36rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 10rpx;
		display: block;
	}
	
	.user-id {
		font-size: 24rpx;
		color: #999;
	}
	
	.edit-btn {
		padding: 10rpx;
	}
	
	.user-points {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding-top: 30rpx;
	}
	
	.points-title {
		font-size: 28rpx;
		color: #666;
	}
	
	.points-value {
		font-size: 32rpx;
		color: #ff6600;
		font-weight: bold;
	}
	
	.menu-section {
		background-color: #fff;
		border-radius: 16rpx;
		padding: 30rpx;
		margin-bottom: 30rpx;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
	}
	
	.section-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
		margin-bottom: 30rpx;
	}
	
	.menu-list {
		display: flex;
		flex-wrap: wrap;
	}
	
	.menu-item {
		width: 25%;
		display: flex;
		flex-direction: column;
		align-items: center;
		margin-bottom: 30rpx;
	}
	
	.menu-icon {
		width: 90rpx;
		height: 90rpx;
		background-color: #f5f5f5;
		border-radius: 50%;
		display: flex;
		justify-content: center;
		align-items: center;
		margin-bottom: 15rpx;
	}
	
	.menu-text {
		font-size: 26rpx;
		color: #666;
	}
	
	.setting-list {
		
	}
	
	.setting-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx 0;
		border-bottom: 1px solid #f0f0f0;
	}
	
	.setting-item:last-child {
		border-bottom: none;
	}
	
	.setting-text {
		font-size: 28rpx;
		color: #333;
	}
	
	.setting-value {
		display: flex;
		align-items: center;
	}
	
	.setting-value text {
		font-size: 28rpx;
		color: #999;
		margin-right: 10rpx;
	}
	
	.logout-btn {
		margin-top: 60rpx;
		height: 90rpx;
		line-height: 90rpx;
		background-color: #fff;
		color: #ff3a30;
		font-size: 32rpx;
		border-radius: 45rpx;
		text-align: center;
		box-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.05);
	}
</style>
